<?php
class ModelCatalogTopic extends Model {
	
	public function getTopics($filter = null) {
		$sql = "SELECT t.*, td.title FROM @@topic t LEFT JOIN @@topic_description td ON t.topic_id=td.topic_id";
		$sql .= " WHERE td.language_id = " . (int)C('config_language_id');
		$sql .= $filter->sort('t.topic_id', 'DESC');
		$sql .= $filter->limit();
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function getTotalTopics($filter = null) {
		return $this->db->queryOne("SELECT COUNT(*) as total FROM @@topic");
	}
	
	public function addTopic($data) {
		$topic_id = $this->db->insert('topic', $data);
		
		foreach ($data['topic_description'] as $language_id => $val) {
			$val['topic_id'] = $topic_id;
			$val['language_id'] = $language_id;
			$this->db->insert('topic_description', $val);
		}
		
		if (isset($data['module'])) {
			foreach ($data['module'] as $module) {
				if ($module['type'] == 'banner' || $module['type'] == 'slideshow') {
					$module['topic_id'] = $topic_id;
					$module['data'] = serialize($module['data']);
					$this->db->insert('topic_module', $module);
				}
				elseif ($module['type'] == 'product') {
					$titles = $module['data']['title'];
					unset($module['data']['title']);
					$module['topic_id'] = $topic_id;					
					$module['data'] = serialize($module['data']);
					$id = $this->db->insert('topic_module', $module);
					
					foreach ($titles as $language_id => $title) {
						$title_data = array(
							'id' => $id,
							'language_id' => $language_id,
							'data' => $title		
						);
						$this->db->insert('topic_module_description', $title_data);
					}
				}
				elseif ($module['type'] == 'custom') {
					$module['topic_id'] = $topic_id;
					$custom_datas = $module['data'];
					$module['data'] = '';
					$id = $this->db->insert('topic_module', $module);
					
					foreach ($custom_datas as $language_id => $val) {
						$custom_data = array(
								'id' => $id,
								'language_id' => $language_id,
								'data' => $val
						);
						$this->db->insert('topic_module_description', $custom_data);
					}
				}
			}
		}
	}
	
	public function editTopic($topic_id, $data) {
		$this->db->update('topic', $data, "topic_id=$topic_id");
		
		$this->db->runSql("DELETE FROM @@topic_description WHERE topic_id = $topic_id");
		foreach ($data['topic_description'] as $language_id => $val) {
			$val['topic_id'] = $topic_id;
			$val['language_id'] = $language_id;
			$this->db->insert('topic_description', $val);
		}
		
		$ids = $this->db->queryArray("SELECT id FROM @@topic_module WHERE topic_id = {$topic_id}");
		$this->db->runSql("DELETE FROM @@topic_module_description WHERE id IN (" . implode(',', $ids) . ")");
		$this->db->runSql("DELETE FROM @@topic_module WHERE topic_id = $topic_id");
		
		if (isset($data['module'])) {
			foreach ($data['module'] as $module) {
				if ($module['type'] == 'banner' || $module['type'] == 'slideshow') {
					$module['topic_id'] = $topic_id;
					$module['data'] = serialize($module['data']);
					$this->db->insert('topic_module', $module);
				}
				elseif ($module['type'] == 'product') {
					$titles = $module['data']['title'];
					unset($module['data']['title']);
					$module['topic_id'] = $topic_id;
					$module['data'] = serialize($module['data']);
					$id = $this->db->insert('topic_module', $module);
						
					foreach ($titles as $language_id => $title) {
						$title_data = array(
								'id' => $id,
								'language_id' => $language_id,
								'data' => $title
						);
						$this->db->insert('topic_module_description', $title_data);
					}
				}
				elseif ($module['type'] == 'custom') {
					$module['topic_id'] = $topic_id;
					$custom_datas = $module['data'];
					$module['data'] = '';
					$id = $this->db->insert('topic_module', $module);
						
					foreach ($custom_datas as $language_id => $val) {
						$custom_data = array(
								'id' => $id,
								'language_id' => $language_id,
								'data' => $val
						);
						$this->db->insert('topic_module_description', $custom_data);
					}
				}
			}
		}
	}
	
	public function getTopic($topic_id) {
		return $this->db->queryOne("SELECT * FROM @@topic WHERE topic_id = $topic_id");
	}
	
	public function getTopicDescription($topic_id) {
		return $this->db->queryArray("SELECT * FROM @@topic_description WHERE topic_id = $topic_id", 'language_id');
	}
	
	public function getTopicModules($topic_id) {
		$query = $this->db->query("SELECT * FROM @@topic_module WHERE topic_id = $topic_id ORDER BY sort_order, id");
		$modules = $query->rows;
		foreach ($modules as $k => $module) {
			if ($module['type'] == 'banner' || $module['type'] == 'slideshow') {
				$modules[$k]['data'] = @unserialize($module['data']);
			}
			elseif ($module['type'] == 'product') {
				$modules[$k]['data'] = @unserialize($module['data']);
				$modules[$k]['data']['title'] = $this->db->queryArray("SELECT * FROM @@topic_module_description WHERE id = {$module['id']}", 'language_id', 'data');
			}
			elseif ($module['type'] == 'custom') {
				$modules[$k]['data'] = $this->db->queryArray("SELECT * FROM @@topic_module_description WHERE id = {$module['id']}", 'language_id', 'data');
			}
		}
		return $modules;
	}
	
	public function deleteTopic($topic_id) {
		$ids = $this->db->queryArray("SELECT id FROM @@topic_module WHERE topic_id = {$topic_id}");
		$this->db->runSql("DELETE FROM @@topic_module_description WHERE id IN (" . implode(',', $ids) . ")");
		$this->db->runSql("DELETE FROM @@topic_module WHERE topic_id = {$topic_id}");
		$this->db->runSql("DELETE FROM @@topic_description WHERE topic_id = {$topic_id}");
		$this->db->runSql("DELETE FROM @@topic WHERE topic_id = {$topic_id}");
	}
}